<template>
    <div class="basicParams pl15 pt10">
      <el-form :disabled="group !== 'tenancy'" label-position="right" label-width="98px" :model="basicParamsForm" :rules="basicParamsFormRules" ref="basicParamsForm">
        <div class="bbd">
          <div class="w100 position_relative">
            <div class="f18 clr_333">DICOM</div>
            <div class="f18 clr_333 mt15">设备工作列表服务（MWL&MPPS SCP）</div>
          </div>
          <div class="w100 mt30 mb10 clear_fixed">
            <el-form-item label="应用标识：" class="fl" prop="mwl_aet">
              <el-input
                size="medium"
                placeholder="请输入应用标识"
                style="width:250px;"
                v-model="basicParamsForm.mwl_aet">
              </el-input>
            </el-form-item>
            <el-form-item label="主机：" class="fl" prop="mwl_host">
              <el-input
                size="medium"
                placeholder="请输入主机"
                style="width:250px;"
                v-model="basicParamsForm.mwl_host">
              </el-input>
            </el-form-item>
            <el-form-item label="端 口 号：" class="fl" prop="mwl_port">
              <el-input
                size="medium"
                placeholder="请输入端口号"
                style="width:250px;"
                v-model="basicParamsForm.mwl_port">
              </el-input>
            </el-form-item>

            <!-- <el-form-item label-width="0" class="fl ml38">
              <el-checkbox v-model="basicParamsForm.mwlIsAutoRegist" :true-label="1" :false-label="0">自动注册</el-checkbox>
            </el-form-item> -->
          </div>
          <div class="clear_fixed w100">
            <el-form-item label="日志保存路径：" class="fl" prop="mwl_log_path">
              <el-input
                size="medium"
                placeholder="请输入保存路径"
                style="width: 695px"
                v-model="basicParamsForm.mwl_log_path">
              </el-input>
            </el-form-item>
            <el-form-item label="日志保存时长：" class="fl ml30" prop="mwl_log_save_days">
              <el-input-number style="width: 100px" size="medium" v-model="basicParamsForm.mwl_log_save_days" controls-position="right" :min="1"></el-input-number>
              <span class="ml10">天</span>
            </el-form-item>
          </div>
          <div class="of w100 mb15">
            <el-form-item label="日志等级：">
              <el-radio v-model="basicParamsForm.mwl_log_mode" :label="0">致命</el-radio>
              <el-radio v-model="basicParamsForm.mwl_log_mode" :label="1">错误</el-radio>
              <el-radio v-model="basicParamsForm.mwl_log_mode" :label="2">警告</el-radio>
              <el-radio v-model="basicParamsForm.mwl_log_mode" :label="3">过程</el-radio>
              <el-radio v-model="basicParamsForm.mwl_log_mode" :label="4">调试</el-radio>
              <el-radio v-model="basicParamsForm.mwl_log_mode" :label="5">跟踪</el-radio>
            </el-form-item>
          </div>
        </div>
        <div class="bbd mt30">
          <div class="f18 clr_333">存储服务（C-Store&Commitment SCP）</div>
          <div class="w100 mt30 mb10 clear_fixed">
            <el-form-item label="应用标识：" class="fl" prop="store_aet">
              <el-input
                size="medium"
                placeholder="请输入应用标识"
                style="width:250px;"
                v-model="basicParamsForm.store_aet">
              </el-input>
            </el-form-item>
            <el-form-item label="主机：" class="fl" prop="store_host">
              <el-input
                size="medium"
                placeholder="请输入主机"
                style="width:250px;"
                v-model="basicParamsForm.store_host">
              </el-input>
            </el-form-item>
            <el-form-item label="端 口 号：" class="fl" prop="store_port">
              <el-input
                size="medium"
                placeholder="请输入端口号"
                style="width:250px;"
                v-model="basicParamsForm.store_port">
              </el-input>
            </el-form-item>

            <!-- <el-form-item label-width="0" class="fl ml38">
              <el-checkbox v-model="basicParamsForm.storeAutoRegist" :true-label="1" :false-label="0">自动注册</el-checkbox>
            </el-form-item> -->
          </div>
          <div class="clear_fixed w100">
            <el-form-item label="日志保存路径：" class="fl" prop="store_log_path">
              <el-input
                size="medium"
                placeholder="请输入保存路径"
                style="width: 695px"
                v-model="basicParamsForm.store_log_path">
              </el-input>
            </el-form-item>
            <el-form-item label="日志保存时长：" class="fl ml30" prop="store_log_save_days">
              <el-input-number style="width: 100px" size="medium" v-model="basicParamsForm.store_log_save_days" controls-position="right" @change="" :min="1"></el-input-number>
              <span class="ml10">天</span>
            </el-form-item>
          </div>
          <div class="of w100 mb15">
            <el-form-item label="日志等级：">
              <el-radio v-model="basicParamsForm.store_log_mode" :label="0">致命</el-radio>
              <el-radio v-model="basicParamsForm.store_log_mode" :label="1">错误</el-radio>
              <el-radio v-model="basicParamsForm.store_log_mode" :label="2">警告</el-radio>
              <el-radio v-model="basicParamsForm.store_log_mode" :label="3">过程</el-radio>
              <el-radio v-model="basicParamsForm.store_log_mode" :label="4">调试</el-radio>
              <el-radio v-model="basicParamsForm.store_log_mode" :label="5">跟踪</el-radio>
            </el-form-item>
          </div>
        </div>
        <div class="bbd mt30">
          <div class="f18 clr_333">查询获取服务（Query&Retrieve SCP）</div>
          <div class="w100 mt30 mb10 clear_fixed">
            <el-form-item label="应用标识：" class="fl" prop="qr_aet">
              <el-input
                size="medium"
                placeholder="请输入应用标识"
                style="width:250px;"
                v-model="basicParamsForm.qr_aet">
              </el-input>
            </el-form-item>
            <el-form-item label="主机：" class="fl" prop="qr_host">
              <el-input
                size="medium"
                placeholder="请输入主机"
                style="width:250px;"
                v-model="basicParamsForm.qr_host">
              </el-input>
            </el-form-item>
            <el-form-item label="端 口 号：" class="fl" prop="qr_port">
              <el-input
                size="medium"
                placeholder="请输入端口号"
                style="width:250px;"
                v-model="basicParamsForm.qr_port">
              </el-input>
            </el-form-item>
          </div>
          <div class="clear_fixed w100">
            <el-form-item label="日志保存路径：" class="fl" prop="qr_log_path">
              <el-input
                size="medium"
                placeholder="请输入保存路径"
                style="width: 695px"
                v-model="basicParamsForm.qr_log_path">
              </el-input>
            </el-form-item>
            <el-form-item label="日志保存时长：" class="fl ml30" prop="qr_log_save_days">
              <el-input-number style="width: 100px" size="medium" v-model="basicParamsForm.qr_log_save_days" controls-position="right" @change="" :min="1"></el-input-number>
              <span class="ml10">天</span>
            </el-form-item>
          </div>
          <div class="of w100">
            <el-form-item label="日志等级：">
              <el-radio v-model="basicParamsForm.qr_log_mode" :label="0">致命</el-radio>
              <el-radio v-model="basicParamsForm.qr_log_mode" :label="1">错误</el-radio>
              <el-radio v-model="basicParamsForm.qr_log_mode" :label="2">警告</el-radio>
              <el-radio v-model="basicParamsForm.qr_log_mode" :label="3">过程</el-radio>
              <el-radio v-model="basicParamsForm.qr_log_mode" :label="4">调试</el-radio>
              <el-radio v-model="basicParamsForm.qr_log_mode" :label="5">跟踪</el-radio>
            </el-form-item>
          </div>
          <div class="of w100 mb15">
            <el-form-item label="自动注册：">
              <el-checkbox v-model="basicParamsForm.qr_is_auto_register" :true-label="1" :false-label="0"></el-checkbox>
              <span class="ml10">（开启后48小时内有效，到期自动关闭）</span>
            </el-form-item>
          </div>
        </div>
        <!-- <div class="bbd mt30">
          <div class="f18 clr_333">云归档查询获取服务（Idcas QR Service）</div>
          <div class="w100 mt30 mb10 clear_fixed">
            <el-form-item label="应用标识：" class="fl" prop="idcasqr_aet">
              <el-input
                size="medium"
                placeholder="请输入应用标识"
                style="width:250px;"
                v-model="basicParamsForm.idcasqr_aet">
              </el-input>
            </el-form-item>
            <el-form-item label="主机：" class="fl" prop="idcas_qr_host">
              <el-input
                size="medium"
                placeholder="请输入主机"
                style="width:250px;"
                v-model="basicParamsForm.idcas_qr_host">
              </el-input>
            </el-form-item>
            <el-form-item label="端 口 号：" class="fl" prop="idcas_qr_port">
              <el-input
                size="medium"
                placeholder="请输入端口号"
                style="width:250px;"
                v-model="basicParamsForm.idcas_qr_port">
              </el-input>
            </el-form-item>
          </div>
          <div class="clear_fixed w100">
            <el-form-item label="日志保存路径：" class="fl" prop="idcas_qr_log_path">
              <el-input
                size="medium"
                placeholder="请输入保存路径"
                style="width: 695px"
                v-model="basicParamsForm.idcas_qr_log_path">
              </el-input>
            </el-form-item>
            <el-form-item label="日志保存时长：" class="fl ml30" prop="idcas_qr_log_save_days">
              <el-input-number style="width: 100px" size="medium" v-model="basicParamsForm.idcas_qr_log_save_days" controls-position="right" :min="1"></el-input-number>
              <span class="ml10">天</span>
            </el-form-item>
          </div>
          <div class="w100 mb15">
            <el-form-item label="日志等级：">
              <el-radio v-model="basicParamsForm.idcas_qr_log_mode" :label="0">致命</el-radio>
              <el-radio v-model="basicParamsForm.idcas_qr_log_mode" :label="1">错误</el-radio>
              <el-radio v-model="basicParamsForm.idcas_qr_log_mode" :label="2">警告</el-radio>
              <el-radio v-model="basicParamsForm.idcas_qr_log_mode" :label="3">过程</el-radio>
              <el-radio v-model="basicParamsForm.idcas_qr_log_mode" :label="4">调试</el-radio>
              <el-radio v-model="basicParamsForm.idcas_qr_log_mode" :label="5">跟踪</el-radio>
            </el-form-item>
          </div>
          <div class="clear_fixed w100 mb15">
            <el-form-item label="云归档访问账号：" label-width="112px" class="fl" prop="idcas_account">
              <el-input
                size="medium"
                placeholder="请输入"
                style="width:180px;"
                v-model="basicParamsForm.idcas_account"
                clearable>
              </el-input>
            </el-form-item>
            <el-form-item label="云归档访问密码：" label-width="112px" class="fl ml30" prop="idcas_password">
              <el-input
                size="medium"
                placeholder="请输入"
                style="width:180px;"
                v-model="basicParamsForm.idcas_password"
                show-password
                clearable>
              </el-input>
            </el-form-item>
          </div>
        </div> -->

        <div class="bbd mt30">
          <div class="f18 clr_333">归档与匹配服务</div>
          <!-- <div class="mt30">
            <el-checkbox size="small"  :false-label="0" :true-label="1">匹配成功时用登记信息(患者姓名、性别、年龄、出生日期)更新影像信息</el-checkbox>
          </div> -->
          <div class="w100 mt30 mb10 clear_fixed">
            <el-form-item label="日志保存时长：" class="fl" prop="log_save_days">
              <el-input-number style="width: 100px" size="medium" v-model="basicParamsForm.log_save_days" controls-position="right" @change="" :min="1"></el-input-number>
              <span class="ml10">天</span>
            </el-form-item>
          </div>
          <div class="clear_fixed w100">
            <el-form-item label="日志保存路径：" class="fl" prop="log_path">
              <el-input
                size="medium"
                placeholder="请输入保存路径"
                style="width: 970px"
                v-model="basicParamsForm.log_path">
              </el-input>
            </el-form-item>
          </div>
          <div class="of w100 mb15">
            <el-form-item label="日志等级：">
              <el-radio v-model="basicParamsForm.log_mode" :label="0">致命</el-radio>
              <el-radio v-model="basicParamsForm.log_mode" :label="1">错误</el-radio>
              <el-radio v-model="basicParamsForm.log_mode" :label="2">警告</el-radio>
              <el-radio v-model="basicParamsForm.log_mode" :label="3">过程</el-radio>
              <el-radio v-model="basicParamsForm.log_mode" :label="4">调试</el-radio>
              <el-radio v-model="basicParamsForm.log_mode" :label="5">跟踪</el-radio>
            </el-form-item>
          </div>
        </div>

        <div class="bbd mt30">
          <div class="f18 clr_333">Dicom Web Service 地址</div>
          <div class="clear_fixed w100 mt30 mb30">
            <el-form-item label="访问地址：" class="fl">
              <el-input
                size="medium"
                placeholder="请输入访问地址"
                style="width: 970px"
                v-model="basicParamsForm.dicom_web_service_url">
              </el-input>
            </el-form-item>
            <el-button type="primary" class="w80 fr mr20" @click="basicParamsSave('basicParamsForm')">保存</el-button>
          </div>
        </div>
        <!-- <div class="mt30">
          <div class="f18 clr_333">RIS服务地址</div>
          <div class="clear_fixed w100 mt30 mb30">
            <el-form-item label="访问地址：" class="fl" prop="ris_web_service_url">
              <el-input
                size="medium"
                placeholder="请输入访问地址"
                style="width: 970px"
                v-model="basicParamsForm.ris_web_service_url">
              </el-input>
            </el-form-item>

          </div>
        </div> -->
      </el-form>
    </div>
</template>

<script>
import { validPort, validPath, validURL, validIP } from '@/utils/validate'
import { mapGetters } from 'vuex'

export default {
  name: 'BasicParams',
  data () {
    let requiredValidator = function (tip) {
      return (rule, value, callback) => {
        if(value === '' || value === null) {
          callback(new Error(tip))
        }else {
          callback()
        }
      }
    }
    let aetValidator = (rule, value, callback) => {
      if(value === '' || value === null) {
        callback(new Error('请输入应用标识'))
      }else {
        callback()
      }
    }
    let portValidator = (rule, value, callback) => {
      if(value !== '') {
        if(validPort(value)) {
          callback()
        }else {
          callback(new Error('端口号范围为0-65535'))
        }
      }else {
        // callback(new Error('请输入端口号'))
        callback()
      }
    }
    // let idcas_qr_portValidator = (rule, value, callback) => {
    //   if(value !== '') {
    //     if(validPort(value)) {
    //       callback()
    //     }else {
    //       callback(new Error('端口号范围为0-65535'))
    //     }
    //   }else {
    //     callback()
    //   }
    // }
    let pathValidator = (rule, value, callback) => {
      if(value !== '') {
        if(validPath(value)) {
          callback()
        }else {
          callback(new Error('日志保存路径输入错误'))
        }
      }else {
        callback()
      }
    }
    let urlValidator = (rule, value, callback) => {
      if(value !== '') {
        if(validURL(value) || validIP(value)) {
          callback()
        }else {
          callback(new Error('访问地址格式错误'))
        }
      }else {
        // callback(new Error('请输入访问地址'))
        callback()
      }
    }
    let idcasQrUrlValidator = (rule, value, callback) => {
      if(value !== '') {
        if(validURL(value) || validIP(value)) {
          callback()
        }else {
          callback(new Error('访问地址格式错误'))
        }
      }else {
        callback()
      }
    }
    let urlNotNeedValidator = (rule, value, callback) => {
      if(value !== '') {
        if(validURL(value) || validIP(value)) {
          callback()
        }else {
          callback(new Error('访问地址格式错误'))
        }
      }else {
        callback()
      }
    }
    let iptNumValidator = (iptNumName) => {
      return (rule, value, callback) => {
        if(value === undefined) {
          callback()
        }else if(!Number.isInteger(value)) {
          this.basicParamsForm[iptNumName] = parseInt(value)
        }else {
          callback()
        }
      }
    }
    let idcasQrIptNumValidator = (iptNumName) => {
      return (rule, value, callback) => {
        if(value === undefined) {
          callback()
        }else if(!Number.isInteger(value)) {
          this.basicParamsForm[iptNumName] = parseInt(value)
        }else {
          callback()
        }
      }
    }
    // requiredValidator('请输入日志保存路径')
    let logPathValidator = (rule, value, callback) => {
      if (value !== '') {
        if(validPath(value)) {
          callback()
        }else {
          callback(new Error('日志保存路径输入错误'))
        }
      } else {
        callback(new Error('请输入日志保存路径'))
      }
    }
    return {
      basicParamsForm: {
        id: '',
        mwl_aet: '',
        mwl_port: '',
        // mwlIsAutoRegist: '',
        mwl_log_path: '',
        mwl_log_save_days: 5,
        mwl_log_mode: '',
        store_aet: '',
        store_port: '',
        // storeAutoRegist: '',
        store_log_path: '',
        store_log_save_days: 5,
        store_log_mode: '',
        qr_aet: '',
        qr_port: '',
        qr_is_auto_register: '',
        qr_log_path: '',
        qr_log_save_days: 5,
        qr_log_mode: '',
        log_path: '',
        log_save_days: 5,
        log_mode: '',
        dicom_web_service_url: '',
        // ris_web_service_url: ''
        archive_match:'',
        // idcas_qr_aet: '',
        // idcas_qr_host: '',
        // idcas_qr_port: '',
        // idcas_qr_log_path: '',
        // idcas_qr_log_save_days: 5,
        // idcas_qr_log_mode: '',
        // idcas_account: '',
        // idcas_password: ''
      },
      basicParamsFormRules: {
        // mwl_aet: [{
        //   trigger: 'blur',
        //   validator: aetValidator
        // }],
        mwl_port: [{
          trigger: 'blur',
          validator: portValidator
        }],
        // mwl_log_path: [{
        //   trigger: 'blur',
        //   validator: requiredValidator('请输入日志保存路径')
        // }],
        mwl_host: [{
          trigger: 'blur',
          validator: urlValidator
        }],
        // store_aet: [{
        //   trigger: 'blur',
        //   validator: aetValidator
        // }],
        store_port: [{
          trigger: 'blur',
          validator: portValidator
        }],
        // store_log_path: [{
        //   trigger: 'blur',
        //   validator: requiredValidator('请输入日志保存路径')
        // }],
        store_host: [{
          trigger: 'blur',
          validator: urlValidator
        }],
        // qr_aet: [{
        //   trigger: 'blur',
        //   validator: aetValidator
        // }],
        qr_port: [{
          trigger: 'blur',
          validator: portValidator
        }],
        // qr_log_path: [{
        //   trigger: 'blur',
        //   validator: requiredValidator('请输入日志保存路径')
        // }],
        qr_host: [{
          trigger: 'blur',
          validator: urlValidator
        }],
        log_path: [{
          trigger: 'blur',
          validator: logPathValidator
        }],
        log_save_days: [{
          trigger: 'blur',
          validator: iptNumValidator('log_save_days')
        }],
        // ris_web_service_url: [{
        //   trigger: 'blur',
        //   validator: urlValidator
        // }],
        mwl_log_save_days: [{
          trigger: 'blur',
          validator: iptNumValidator('mwl_log_save_days')
        }],
        store_log_save_days: [{
          trigger: 'blur',
          validator: iptNumValidator('store_log_save_days')
        }],
        qr_log_save_days: [{
          trigger: 'blur',
          validator: iptNumValidator('qr_log_save_days')
        }],
        // idcasqr_aet: [{
        //   trigger: 'blur',
        //   validator: aetValidator
        // }],
        // idcas_qr_host: [{
        //   trigger: 'blur',
        //   validator: idcasQrUrlValidator
        // }],
        // idcas_qr_port: [{
        //   trigger: 'blur',
        //   validator: idcas_qr_portValidator
        // }],
        // idcas_qr_log_path: [{
        //   trigger: 'blur',
        //   validator: requiredValidator('请输入日志保存路径')
        // }],
        // idcas_qr_log_save_days: [{
        //   trigger: 'blur',
        //   validator: idcasQrIptNumValidator('idcas_qr_log_save_days')
        // }],
        // idcas_account: [{
        //   trigger: 'blur',
        //   validator: requiredValidator('请输入')
        // }],
        // idcas_password: [{
        //   trigger: 'blur',
        //   validator: requiredValidator('请输入')
        // }]
      }
    }
  },
  computed: {
    ...mapGetters(['group'])
  },
  methods: {
    async getBasicParams () {
      this.$pacsApi.pacsApi.getBasicParams().then(res => {
        const { code, data } = res
        if (code === 0) {
          this.basicParamsForm = data
        }
      })
    },
    async saveBasicParams () {
      const res = await this.$pacsApi.pacsApi.saveBasicParams(this.basicParamsForm)
      const { code, msg } = res
      if (code === 0) {
        this.$message({
          type: 'success',
          message: msg
        })
        this.getBasicParams()
      }
    },
    basicParamsSave (formName) {
      this.$refs[formName].validate((valid) => {
        if(valid) {
          this.saveBasicParams()
        }else {
          return false
        }
      })
    }
  },
  mounted () {
    this.getBasicParams()
  }
}
</script>

<style lang="less" scoped>
  /deep/.el-form-item__label {
    padding-right: 0;
  }
</style>
